﻿using BasicInformation.Domain;
using BasicInformation.Infrastructure.Interface;
using Microsoft.Extensions.Configuration;
using MySql.Data.MySqlClient;
using SqlSugar;
using System.Data;
using DbType = SqlSugar.DbType;

namespace BasicInformation.Infrastructure.Implement
{
    public class DatabaseContext : IDatabaseContext
    {
        private readonly IConfiguration _configuration;
        private readonly string _connectionString;
        public SqlSugarScope SqlSugar { get; }
        public IDbConnection DapperConnection { get; }

        public SqlSugarScope SqlSugarScope => throw new NotImplementedException();

       

        public DatabaseContext(IConfiguration configuration)
        {
            _configuration = configuration;
            _connectionString = configuration.GetValue<string>("wmss");
            SqlSugar = new SqlSugarScope(new ConnectionConfig()
            {
                DbType = DbType.MySql,
                ConnectionString = _connectionString,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute,
            },
            db =>
            {
                db.DbMaintenance.CreateDatabase("WMSRepository");
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine(sql);
                };
            });
            DapperConnection = new MySqlConnection(_connectionString);
        }
        public void InitializeDatabase()
        {
            try
            {
                //// 初始化所有表
                //SqlSugar.CodeFirst.InitTables(
                ////////////// 你的实体类
                ////////////typeof(CarrierModel),
                ////////////typeof(CategoryModel),
                ////////////typeof(ClientModel),
                ////////////typeof(DepotModel),
                ////////////typeof(DepotTypeModel),
                ////////////typeof(DeviceModel),
                ////////////typeof(LocationModel),
                ////////////typeof(LocationTypeModel),
                ////////////typeof(ProductModel),
                ////////////typeof(SupplierModel),
                ////////////typeof(SupplierTypeModel),
                ////////////typeof(UnitModel)
                //////typeof(InventorysModel)
                //////typeof(ResultModel),
                //////typeof(ITypeModel),
                ////typeof(InboundModel)
                //typeof(OperationModel)
                //);

                Console.WriteLine("数据库表初始化完成");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"数据库初始化失败: {ex.Message}");
                throw; // 根据你的需求决定是否抛出异常
            }
        }
    }
}
